<?php
class PDtBdsController extends AppController {

	var $name = 'PDtBds';
	var $helpers = array('Html', 'Form', 'Ajax');
	var $uses = array('Canbo', 'User', 'Trambvtv', 'PDtBd', 'Bayden', 'ChicucBvtv', 'Doituong', 'KqBd', 'DtLoaidt');
	var $paginate = array('limit' =>30, 'page' => 1);
	var $components = array('Session','Cookie');
   	
    function index(){
        $tram_id = null;
        $canbo_id = null;
        
        if(!empty($this->data)){
            $this->Session->delete('PDtBdIndex');
            if(empty($this->data['PDtBd']['reset'])){
                if(!empty($this->data['PDtBd']['canbo_id'])){
                    $canbo_id = $this->data['PDtBd']['canbo_id'];
                    $this->Session->write('PDtBdIndex.canbo_id', $canbo_id);
                }
                $tram_id = $this->data['PDtBd']['tram_id'];
                $this->Session->write('PDtBdIndex.tram_id', $tram_id);
            }
        }else{
            if($this->Session->check('PDtBdIndex.tram_id')){
                $tram_id = $this->Session->read('PDtBdIndex.tram_id');
            }
            if($this->Session->check('PDtBdIndex.canbo_id')){
                $canbo_id = $this->Session->read('PDtBdIndex.canbo_id');
            }
        }
        
        if(!empty($tram_id)){
            if(!empty($canbo_id)){
                $results = $this->paginate('PDtBd', array('PDtBd.canbo_id'=>$canbo_id));                
            }else{
                $cbs = $this->Canbo->find('list', array('conditions'=>array('trambvtv_id'=>$tram_id),
                                                        'fields'=>array('Canbo.id')));
                $results = $this->paginate('PDtBd', array('PDtBd.canbo_id'=>$cbs));
            }
            $canbos = $this->Canbo->find('list',array('conditions'=>array('trambvtv_id'=>$tram_id),
                                                        'fields'=>array('Canbo.id','HOTEN')));
            $this->set(compact('canbos'));
        }else{
            $results = $this->paginate('PDtBd');
        }
        
        $trams = $this->Trambvtv->find('list',array('order'=>array('Trambvtv.TEN_TR asc'),
                                                'fields'=>array('Trambvtv.id','TEN_TR')));
        $this->set('tram_id', $tram_id);
        $this->set('canbo_id', $canbo_id);
        $this->set(compact('trams', 'results'));
    }
    
    function index4(){
        
        if(!empty($this->data)){
            $bien=$this->data['PDtBd']['canbo_id'];
            
        }
        else $bien=''; 
        $this->Cookie->write('PDtBd.canbo_id',$bien,false,60);
        
        if($bien!=''){
      //      $hcThuocs = $this->paginate('HcThuoc',array('HcThuoc.thuoc_id'=>$id));
            $this->set('results', $this->paginate('PDtBd',
                        array('PDtBd.canbo_id'=>$this->Cookie->read('PDtBd.canbo_id'))));
        }else{
          //  $this->set('results', $this->paginate('PDtBd', array('order'=>array('NGAY_PBD'=>'desc'))));
          $this->set('results', $this->paginate('PDtBd'));
            //$results=$this->Canbo->Query("select * from p_dt_bds order by NGAY_PBD desc");
     //        $this->set('results', $this->paginate('PDtBd'));
        }
    //    $canbos = $this->Canbo->find('list',array('fields'=>array('Canbo.id','HOTEN'),
      //                                          'order'=>'HOTEN asc'));
          $canbos = $this->Canbo->find('list',array('fields'=>array('Canbo.id','HOTEN')));
    //    $canbos = array();	
		$trams = $this->Trambvtv->find('list',array('order'=>array('Trambvtv.TEN_TR asc'),
        'fields'=>array('Trambvtv.id','TEN_TR')
                                                ));
	//	$trams = $this->Trambvtv->find('list',array(        'fields'=>array('Trambvtv.id','TEN_TR'),
        //                                        ));
        $this->set(compact('trams', 'canbos','results'));
    }
    
    function update_tram_select(){
        if(!empty($this->data['PDtBd']['tram_id'])){
             $options = $this->Canbo->find('list', 
                            array('conditions'=>array('trambvtv_id'=>$this->data['PDtBd']['tram_id']),
                            'fields'=>array('Canbo.id','HOTEN')));
                
            $this->set('options', $options);
        }else{
            $this->set('options', array());
        }
    }
    
	function index3 () { 		
        if(empty($condition1s)){
            if (!empty($this->data)) {
    		    $tram=$this->data['PDtBd']['tuyen'];
                $canb=$this->data['PDtBd']['canbo'];
                $dk=$this->data['PDtBd']['chon'];
                
                if ($dk=='t'){
                    $conditions['Canbo.trambvtv_id = ']=$tram;
    			}else{
                    $conditions['PDtBd.canbo_id =']=$canb;
                }
                              
                $this->Session->write('PDtBd.cond', $conditions); 
                //$condition1s = $this->Session->read('PDtBd.cond');
            }    
        }
        $this->PDtBd->recursive=2;
        $canbos = $this->PDtBd->Canbo->find('list',array('fields'=>array('Canbo.id','HOTEN'),'order'=>array('HOTEN'=>'asc')));	
		$tuyens = $this->PDtBd->Canbo->Trambvtv->find('list',array('fields'=>array('Trambvtv.id','TEN_TR'),'order'=>array('TEN_TR'=>'asc')));         
        $this->set('results', $this->paginate('PDtBd',$this->Session->read('PDtBd.cond')));
        //print_r($condition1s);	
		$this->set(compact('canbos','tuyens','conditions','tram','canb','dk','results','condition1s','PDtBd.cond'));	
	}
    
	function index2 () { 
			
		if(empty($this->data)){		
		$condition='1=0';
		/**
 * $this->Cookie->write('cb',0,false,300);
 * 		$this->Cookie->write('t',0,false,300);
 * 		$this->Cookie->write('nl',0,false,300);
 * 		$this->Cookie->write('nn',0,false,300);
 */
		
		}else{
 	          if( $this->Cookie->read('t')!="" && $this->Cookie->read('cb')!="")
                    {    	
                        $this->Cookie->write('cb',0,false,300);
                        $this->Cookie->write('t',0,false,300);
		                
                    }
			
			$this->PDtBd->recursive = 2;
		
			$tam='';
        if ($this->data['PDtBd']['chon']=='t')
            {

            $this->Cookie->write('t',$this->data ['PDtBd']['chon'],false,300);
		 	$this->Cookie->write('tuyen',$this->data ['PDtBd']['tuyen'],false,300);
			$datay = $this->Canbo->find('list',array('conditions'=>array('Canbo.trambvtv_id'=> $this->Cookie->read('tuyen') ),'recursive' => 0,'contain' => array('Trambvtv'),'fields' => array('Canbo.id')));
            }
            else {if ($this->data['PDtBd']['chon']=='h')
                {
    		      $this->Cookie->write('t',$this->data ['PDtBd']['chon'],false,300);
                  $this->Cookie->write('canbo',$this->data ['PDtBd']['canbo'],false,300);
		          //$datay=$this->data['PDtBd']['canbo'];
                  $datay=$this->Cookie->read('canbo');
                  }
            }
            			}

        $this->data['PDtBd']['ngaylap'] = $_POST['ngaylap'];
        //$this->set('results', $this->paginate('PDtBd',array('PDtBd.canbo_id'=>$datay,'ngay_pbd'=>$this->data['PDtBd']['ngaylap'])));
        $this->set('results', $this->paginate('PDtBd',array('PDtBd.canbo_id'=>$datay)));
    

		$canbos = $this->PDtBd->Canbo->find('list',array('fields'=>array('Canbo.id','HOTEN'),'order'=>array('HOTEN'=>'asc')));	
		$tuyens = $this->PDtBd->Canbo->Trambvtv->find('list',array('fields'=>array('Trambvtv.id','TEN_TR'),'order'=>array('TEN_TR'=>'asc')));	
		$this->set(compact('canbos','tuyens','datay'));	
		
		
	}

    
    
    function index1() {
	
		// Vi du lay ten can bo
        $this->User->unbindModel(array('hasMany' => array('KqBd')));
		$this->User->recursive = 1;
		$cb = $this->User->find('first',array('fields'=>array('Canbo.id','Canbo.HOTEN'),'conditions'=>array('User.id'=>$this->Auth->user('id'))));
		//$this->Trambvtv->recursive = 1;
		$tram = $this->Canbo->find('first',array('conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
		$this->set('cb',$cb);
		$this->set('tram',$tram);
		// Kêt thuc vi du
			$this->set('tramsa', $this->Trambvtv->findAll());
			$this->set('canbosa', $this->Canbo->findAll());
            

            
            $this->set('trams', $this->Trambvtv->findById($this->data['PDtBd']['TINH']));
			$this->set('canbos', $this->Canbo->findByid($this->data['PDtBd']['DOITUONG']));
            
            $this->Cookie->write('TINH',$this->data['PDtBd']['TINH'],false,300);
            $this->Cookie->write('DOITUONG',$this->data['PDtBd']['DOITUONG'],false,300);
            
            if ($this->data['PDtBd']['DOITUONG']=='')
            {
			$datay = $this->Canbo->find('list',
	       	array('conditions'=>array('Canbo.trambvtv_id'=> $this->data['PDtBd']['TINH'] ),'recursive' => 0,'contain' => array('Trambvtv'),
									'fields' => array('Canbo.id')
			) );
            }
            else 
            $datay=$this->data['PDtBd']['DOITUONG'];
             //  $this->Cookie->write('trams',$this->data ['PDtBds']['TINH'],false,300);
			//	$this->Cookie->write('canbos',$this->data ['PDtBds']['DOITUONG'],false,300);
				$this->Cookie->write('PDtBd',1,false,300);
               // $this->Cookie->read('trams');
                //$this->Cookie->read('canbos');
//                $this->Cookie->read('datay');
                            
				//$this->Cookie->write('cauh',$this->data ['Tkc']['cauh'],false,300);
				//$this->Cookie->write('huyen',$this->data ['Tkc']['huyen'],false,300);
		//$this->PDtBd->recursive = 1;
	//	if($this->Auth->user('ROLE')=='admin'){
	//		$this->set('pDtBds', $this->paginate('PDtBd'));
	//	}else{
	   
			$this->set('pDtBds', $this->paginate('PDtBd',array('PDtBd.canbo_id'=>$datay)));

///		}
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash('Phiếu điều ta bẫy đèn không hợp lệ');
			$this->redirect(array('action'=>'index'));
		}
		$this->User->recursive = 1;
		$cb = $this->User->find('first',array('fields'=>array('Canbo.id','Canbo.HOTEN'),'conditions'=>array('User.id'=>$this->Auth->user('id'))));
		
		$this->Canbo->recursive = 1;
		$tram = $this->Canbo->find('first', array('fields'=>array('Trambvtv.TEN_TR', 'Trambvtv.id'), 'conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
		
		$this->Trambvtv->recursive = 1;
		$chicuc = $this->Trambvtv->find('first', array('fields'=>array('ChicucBvtv.TEN_CC', 'ChicucBvtv.id'), 'conditions'=>array('Trambvtv.id'=>$tram['Trambvtv']['id'])));
		
		$this->Doituong->recursive = 1;
		$doituongs = $this->Doituong->KqBd->find('all', array('conditions'=>array('KqBd.P_Dt_Bd_id'=>$id)));
		if(empty($doituongs)){// neu chua nhap du lieu
			$doituongs = $this->Doituong->find('all');
		}
		$phieu = $this->PDtBd->find('first',array('conditions'=>array('PDtBd.id'=>$id)));
		$this->set(compact('cb', 'tram', 'chicuc', 'doituongs', 'phieu'));
	}

	function xuatpdf($id = null) {
		if (!$id) {
			$this->Session->setFlash('Thao tác không hợp lệ');
			$this->redirect(array('controller'=>'pages', 'action'=>'home'));
		}
		
		App::import('Vendor', 'bayden_xtcpdf');
		 
		$mypdf = new BAYDEN_XTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
		$this->layout = null;
		
		//set margins
		// set document information
		$mypdf->SetCreator(PDF_CREATOR);
		$mypdf->SetAuthor('mt');
		$mypdf->SetTitle('KC.01.15/06-10');
		$mypdf->SetSubject('');
		$mypdf->SetKeywords('TCPDF, PDF, example, test, guide');
		
		// set default header data
		//$mypdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
		$mypdf->SetHeaderData('', '', 'KC.01.15/06-10', '');
		// set header and footer fonts
		$mypdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
		$mypdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
		
		// set default monospaced font
		$mypdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
	
		//set margins
		$mypdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
		$mypdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$mypdf->SetFooterMargin(PDF_MARGIN_FOOTER);
		$mypdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
		$mypdf->SetFont('freesans', '', 12); 
		$mypdf->AddPage();
	
		//Lay thong tin can bo
		
		$this->User->recursive = 1;
		$cb = $this->User->find('first',array('conditions'=>array('User.id'=>$this->Auth->user('id'))));
	
		//thong tin phieu dieu tra
		$this->PDtBd->recursive = 1;
		$PDtBds = $this->PDtBd->find('first',array('conditions'=>array('PDtBd.canbo_id'=>$cb['Canbo']['id'])));
		
		//lay thong tin tram bvtv
		$this->Canbo->recursive = 1;
		$tram = $this->Canbo->find('first', array('fields'=>array('Trambvtv.TEN_TR', 'Trambvtv.id'), 'conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
		
		$this->Trambvtv->recursive = 1;
		$chicuc = $this->Trambvtv->find('first', array('fields'=>array('ChicucBvtv.TEN_CC', 'ChicucBvtv.id'), 'conditions'=>array('Trambvtv.id'=>$tram['Trambvtv']['id'])));
		
		$this->Doituong->recursive = 1;
		$doituongs = $this->Doituong->KqBd->find('all', array('conditions'=>array('KqBd.P_Dt_Bd_id'=>$id)));
		if(empty($doituongs)){// neu chua nhap du lieu
			$doituongs = $this->Doituong->find('all');
		}
		$phieu = $this->PDtBd->find('first',array('conditions'=>array('PDtBd.id'=>$id)));
		$this->set(compact('cb', 'tram', 'chicuc', 'doituongs', 'phieu'));
		$mypdf->writeHTML('<b><h2>BIỂU ĐIỀU TRA BẪY ĐÈN</h2></b>', false, false, false, false, 'C');
		$mypdf->writeHTML('----------------------', true, false, false, false, 'C');
		$mypdf->Cell(30 ,7 ,'* Chi cục BVTV: '.$chicuc['ChicucBvtv']['TEN_CC'], 0,1, 'L',0);
		$mypdf->Cell(30 ,7 ,'* Trạm BVTV: '.$tram['Trambvtv']['TEN_TR'], 0,1, 'L',0);
		$mypdf->Cell(30 ,7 ,'* Cán bộ điều tra: '.$cb['Canbo']['HOTEN'], 0,1, 'L',0);
		$mypdf->Cell(30 ,7 ,'* Ngày điều tra: '.date('d-m-Y', strtotime($PDtBds['PDtBd']['NGAY_PBD'])), 0,1, 'L',0);

		// Lấy dữ liệu
		
		$this->Doituong->recursive = 1;
		$doituongs = $this->Doituong->KqBd->find('all', array('conditions'=>array('KqBd.P_Dt_Bd_id'=>$id)));
		if(empty($doituongs)){// neu chua nhap du lieu
			$doituongs = $this->Doituong->find('all');
		}
		$phieu = $this->PDtBd->find('first',array('conditions'=>array('PDtBd.id'=>$id)));
		
	
	
		$this->set(compact('cb', 'tram', 'chicuc', 'doituongs', 'phieu','KqBds'));
		
		$mypdf->writeHTML($mypdf->printPdts($doituongs), true, false, false, false, '');
		
		$mypdf->Output('pdt_bayden.pdf', 'D');
	}

	function add() {				
		if (!empty($this->data)) {
			$this->PDtBd->create();
			if ($this->PDtBd->save($this->data)) {
				$this->Session->setFlash('Phiếu điều tra bẫy đèn đã được tạo. Bạn cần nhập thông số cho phiếu này.');	
				$idtemp1 = $this->PDtBd->id;//luu id moi duoc tao
			//nhay qua trang nhap thong tin
				$this->redirect(array('action'=>'nhapthongtin',$idtemp1));
				//$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash('Không tạo được phiếu điều tra. Bạn hãy thử lại');
			}
		}
		$canbos = $this->PDtBd->Canbo->find('list',array('fields'=>array('Canbo.id','HOTEN')));
		//$baydens = $this->PDtBd->Bayden->find('list',array('fields'=>array('Bayden.id','HDO','TDO')));
		$baydens = $this->PDtBd->Bayden->find('list', array('fields'=>array('id','TEN_BD')));
		$this->set(compact('canbos', 'baydens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash('Phiếu điều tra bẫy đèn không hợp lệ');
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->PDtBd->save($this->data)) {
				$this->Session->setFlash(__('The PDtBd has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Không tạo được phiếu điều tra. Bạn hãy thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->PDtBd->read(null, $id);
		}
		/*
		$canbos = $this->PDtBd->Canbo->find('list');
		$baydens = $this->PDtBd->Bayden->find('list');
		*/
		$canbos = $this->PDtBd->Canbo->find('list',array('fields'=>array('Canbo.id','HOTEN')));
		//$baydens = $this->PDtBd->Bayden->find('list',array('fields'=>array('Bayden.id','HDO','TDO')));
		$baydens = $this->PDtBd->Bayden->find('list', array('fields'=>array('id','TEN_BD')));
		$this->set(compact('canbos','baydens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash('Phiếu điều ta bẫy đèn không hợp lệ');
			$this->redirect(array('action'=>'index'));
		}
		if ($this->PDtBd->del($id)) {
			$this->Session->setFlash('Phiếu điều tra bẫy đèn đã được xóa');
			$this->redirect(array('action'=>'index'));
		}
	}
	
	function nhapthongtin($id = null){
		if (!$id) {
			$this->Session->setFlash('Phiếu điều tra bẫy đèn không hợp lệ');
			$this->redirect(array('action'=>'index'));
		}
		
		if (!empty($this->data)){
			//print_r($this->data);
			$count = count($this->data['madoituong']);//dem so dong 
			if($count == 0){
				$this->Session->setFlash('Không có đối tượng trong phiếu điều tra bẫy đèn');
				$this->redirect(array('action'=>'index'));
			}else

			 if($count == 1){
				if(isset($this->data['maketqua']))
			//trong truong hop sua thi can toi (1)
					$record['KqBd']['id'] = $this->data['maketqua'][0];// (1)
				//(2) (3) la cac thuoc tinh khoa ngoai
				$record['PDtBd']['id'] = $id; //(2)
				$record['Doituong']['id'] = $this->data['madoituong'][0]; //(3)
				$record['KqBd']['SL_TREN_BD'] = $this->data['ketqua'][0];
				$record['KqBd']['GHICHU'] = $this->data['ghichu'][0];
				$this->KqBd->create();
				
				if(!$this->KqBd->saveAll($record)){
					$this->Session->setFlash('Có lỗi xảy ra. Lưu không thành công các thông tin phiếu điều tra bẫy đèn');
				}
				
			}

			else{
				for($i = 0; $i < $count; $i++){
					if(isset($this->data['maketqua']))
						$record['KqBd']['id'] = $this->data['maketqua'][$i];
					$record['PDtBd']['id'] = $id;
					//KHOA NGOAI CUA DOI TUONG
					$record['Doituong']['id'] = $this->data['madoituong'][$i];
					$record['KqBd']['SL_TREN_BD'] = $this->data['ketqua'][$i];
					$record['KqBd']['ghichu'] = $this->data['ghichu'][$i];
					//print_r($record);
					$this->KqBd->create();
					if(!$this->KqBd->saveAll($record)){
						$this->Session->setFlash('Có lỗi xảy ra. Lưu không thành công các thông tin phiếu điều tra bẫy đèn');
					}
				}
			}
			
			$this->Session->setFlash('Lưu thành công các thông tin phiếu điều tra bẫy đèn');			
			$this->redirect(array('action'=>'index'));
		}else{ //trong truong hop du lieu bi rong (empty)
			$this->User->recursive = 1;
			//lay cai dau tien theo dieu kien Cb dang nhap
			$cb = $this->User->find('first',array('fields'=>array('Canbo.id','Canbo.HOTEN'),'conditions'=>array('User.id'=>$this->Auth->user('id'))));
			//xet xem cb khac co sua			
			if($this->PDtBd->find('count', array('conditions'=>array('canbo_id'=>$cb['Canbo']['id'], 'PDtBd.id'=>$id))) == 0){
				$this->Session->setFlash('Bạn không được phép sửa phiếu điều tra của cán bộ khác');			
				$this->redirect(array('action'=>'index'));
			}
			
			$this->Canbo->recursive = 1;
			$tram = $this->Canbo->find('first', array('fields'=>array('Trambvtv.TEN_TR', 'Trambvtv.id'), 'conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
			
			$this->Trambvtv->recursive = 1;
			$chicuc = $this->Trambvtv->find('first', array('fields'=>array('ChicucBvtv.TEN_CC', 'ChicucBvtv.id'), 'conditions'=>array('Trambvtv.id'=>$tram['Trambvtv']['id'])));
			
			$this->Doituong->recursive = 1;
			$doituongs = $this->Doituong->KqBd->find('all', array('conditions'=>array('KqBd.P_Dt_Bd_id'=>$id)));
	//		print_r($doituongs);
			if(empty($doituongs)){// neu chua nhap du lieu
				$this->DtLoaidt->recursive = 1;
				$doituongs = $this->DtLoaidt->find('all', array('conditions'=>array('LoaiDt.TEN_LOAI'=>'Bẫy đèn')));
			}			
			//print_r($doituongs);
			$phieu = $this->PDtBd->find('first',array('conditions'=>array('PDtBd.id'=>$id)));
			$this->set(compact('cb', 'tram', 'chicuc', 'doituongs', 'phieu'));
		}
	}
	 function __view($id = null) {
              if (!$id) {
			$this->Session->setFlash('Phiếu điều ta bẫy đèn không hợp lệ');
			$this->redirect(array('action'=>'index'));
		}
		$this->User->recursive = 1;
		$cb = $this->User->find('first',array('fields'=>array('Canbo.id','Canbo.HOTEN'),'conditions'=>array('User.id'=>$this->Auth->user('id'))));
		
		$this->Canbo->recursive = 1;
		$tram = $this->Canbo->find('first', array('fields'=>array('Trambvtv.TEN_TR', 'Trambvtv.id'), 'conditions'=>array('Canbo.id'=>$cb['Canbo']['id'])));
		
		$this->Trambvtv->recursive = 1;
		$chicuc = $this->Trambvtv->find('first', array('fields'=>array('ChicucBvtv.TEN_CC', 'ChicucBvtv.id'), 'conditions'=>array('Trambvtv.id'=>$tram['Trambvtv']['id'])));
		
		$this->Doituong->recursive = 1;
		$doituongs = $this->Doituong->KqBd->find('all', array('conditions'=>array('KqBd.P_Dt_Bd_id'=>$id)));
		if(empty($doituongs)){// neu chua nhap du lieu
			$doituongs = $this->Doituong->find('all');
		}
		$phieu = $this->PDtBd->find('first',array('conditions'=>array('PDtBd.id'=>$id)));
		$this->set(compact('cb', 'tram', 'chicuc', 'doituongs', 'phieu','id'));
    } 

    function view_pdf($id = null) {
        $this->layout = 'pdf'; //this will use the pdf.ctp layout
        $this->render();
    }

}
?>